package com.zzh.lib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zzh.lib.annotation.Visibility;
import com.zzh.lib.dialog.R;
import com.zzh.lib.views.loader.HImageLoader;

/**
 * Created by ZZH on 2022/4/24.
 *
 * @Date: 2022/4/24
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 带红点的radiobutton
 */
public class HBadgeRadioButton extends FrameLayout {
    private Context context;
    //图片
    private ImageView mTopImageIcon;
    //展示文字
    private TextView textView;
    //提醒数字
    private TextView badgeView;

    private final int id = 0;// 判断是否选中

    int unSelect, select;
    String text;
    boolean isSelected = false, zhNetIcon = false;

    private int selectTextColor = Color.BLUE, unSelectTextColor = Color.BLACK, badgeTextColor = Color.WHITE, badgeBgColor = Color.parseColor("#e84d3e"),
            textSize = 14, badgeTextSize = 10, badgeIconDis = 8, iconSize = 25;
    private Drawable mSelectDrawable, mUnSelectDrawable;
    private String mSelectUrl, mUnSelectUrl;


    @Deprecated
    public void setRadioButton(String text, int unSelect, int select, OnClickListener onClickListener) {
        this.text = text;
        this.unSelect = unSelect;
        this.select = select;
        textView.setText(text);
        if (isSelected) {
            mTopImageIcon.setBackgroundResource(select);
            textView.setTextColor(selectTextColor);
        } else {
            mTopImageIcon.setBackgroundResource(unSelect);
            textView.setTextColor(unSelectTextColor);
        }
        if (onClickListener != null) {
            this.setOnClickListener(onClickListener);
        }
    }

    public HBadgeRadioButton setSelectDrawable(Drawable selectDrawable) {
        mSelectDrawable = selectDrawable;
        return this;
    }

    public HBadgeRadioButton setUnSelectDrawable(Drawable unSelectDrawable) {
        mUnSelectDrawable = unSelectDrawable;
        return this;
    }

    public HBadgeRadioButton setUnSelectTextColor(int unSelectTextColor) {
        this.unSelectTextColor = unSelectTextColor;
        return this;
    }


    @Override
    public void setSelected(boolean b) {
        this.isSelected = b;
        updateTextWidget();
    }

    @Override
    public boolean isSelected() {
        return isSelected;
    }

    /***
     * 改变图片
     */
    public void setImage(Drawable drawable) {
        if (drawable != null) {
            mTopImageIcon.setBackground(drawable);
        }
    }

    /***
     * 设置文本
     *
     * @param num
     */

    public void setNum(int num) {
        if (num < 1) {
            badgeView.setVisibility(View.GONE);
        } else {
            badgeView.setVisibility(View.VISIBLE);
            if (num > 99) {
                badgeView.setText("99+");
            } else {
                badgeView.setText(String.valueOf(num));
            }
        }
    }

    /***
     * 获取文本
     *
     */

    public String getNum() {
        String num = badgeView.getText().toString().trim();
        if (null == num || "".equals(num)) {
            num = "0";
        }

        return num;
    }

    public HBadgeRadioButton setText(String text) {
        this.text = text;
        return this;
    }

    public HBadgeRadioButton setSelectTextColor(int color) {
        selectTextColor = color;
        return this;
    }

    public HBadgeRadioButton setSelectUrl(String selectUrl) {
        mSelectUrl = selectUrl;
        return this;
    }

    public HBadgeRadioButton setUnSelectUrl(String unSelectUrl) {
        mUnSelectUrl = unSelectUrl;
        return this;
    }

    public void update() {
        textView.setText(text);
        updateTextWidget();
    }

    public String getText() {
        return id == 0 ? "" : textView.getText().toString();
    }

    public HBadgeRadioButton(Context context) {
        this(context, null);
    }

    public HBadgeRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);

    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.h_badge_rb, this, true);
        if (isInEditMode()) {
            return;
        }
        mTopImageIcon = findViewById(R.id.iv_radio_item);
        textView = findViewById(R.id.tv_radio_item);
        badgeView = findViewById(R.id.tv_radio_num);
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.HBadgeRadioButton);
        selectTextColor = mTypedArray.getColor(R.styleable.HBadgeRadioButton_zh_select_text_color, Color.RED);
        textSize = mTypedArray.getDimensionPixelSize(R.styleable.HBadgeRadioButton_zh_text_size, 14);
        badgeTextColor = mTypedArray.getColor(R.styleable.HBadgeRadioButton_zh_badge_text_color, Color.GREEN);
        badgeBgColor = mTypedArray.getColor(R.styleable.HBadgeRadioButton_zh_badge_color, Color.GREEN);
        badgeTextSize = mTypedArray.getDimensionPixelSize(R.styleable.HBadgeRadioButton_zh_badge_text_size, 10);
        iconSize = mTypedArray.getDimensionPixelSize(R.styleable.HBadgeRadioButton_zh_icon_size, 58);
        unSelectTextColor = mTypedArray.getColor(R.styleable.HBadgeRadioButton_zh_un_select_text_color, 14);
        badgeIconDis = mTypedArray.getDimensionPixelSize(R.styleable.HBadgeRadioButton_zh_text_icon_size, 8);
        mSelectDrawable = mTypedArray.getDrawable(R.styleable.HBadgeRadioButton_zh_icon_selected);
        mUnSelectDrawable = mTypedArray.getDrawable(R.styleable.HBadgeRadioButton_zh_icon_unselect);
        text = mTypedArray.getString(R.styleable.HBadgeRadioButton_zh_text);
        isSelected = mTypedArray.getBoolean(R.styleable.HBadgeRadioButton_zh_select, false);
        zhNetIcon = mTypedArray.getBoolean(R.styleable.HBadgeRadioButton_zh_icon_net, false);
        mSelectUrl = mTypedArray.getString(R.styleable.HBadgeRadioButton_zh_icon_select_url);
        mUnSelectUrl = mTypedArray.getString(R.styleable.HBadgeRadioButton_zh_icon_unselect_url);

        mTypedArray.recycle();

        drawBadge();
        drawTextWidget();
        drawIconWidget();
        updateTextWidget();
        setOnClickListener(v -> {
            isSelected = !isSelected;
            updateTextWidget();
            if (mOnCheckChangeListener != null) {
                mOnCheckChangeListener.onCheckChange(HBadgeRadioButton.this, isSelected);
            }
        });
    }

    private void drawIconWidget() {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mTopImageIcon.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.width = iconSize;
            layoutParams.height = iconSize;
        }
        mTopImageIcon.setLayoutParams(layoutParams);
    }

    private void drawBadge() {
        GradientDrawable gd = new GradientDrawable();
        gd.setCornerRadius(20);
        gd.setColor(badgeBgColor);
        badgeView.setBackground(gd);
        badgeView.setTextColor(badgeTextColor);
        badgeView.setTextSize(TypedValue.COMPLEX_UNIT_PX, badgeTextSize);
    }

    private void drawTextWidget() {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textView.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.topMargin = badgeIconDis;
        }
        textView.setLayoutParams(layoutParams);

        textView.setText(text);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    }

    void updateTextColor() {
        if (isSelected) {
            textView.setTextColor(selectTextColor);
        } else {
            textView.setTextColor(unSelectTextColor);
        }
    }

    void updateTopImage() {
        if (zhNetIcon) {
            if (isSelected) {
                if (mImageLoader != null) {
                    mImageLoader.loadImage(mTopImageIcon, mSelectUrl);
                }
            } else {
                if (mImageLoader != null) {
                    mImageLoader.loadImage(mTopImageIcon, mUnSelectUrl);
                }
            }
        } else {
            if (isSelected) {
                mTopImageIcon.setImageDrawable(mSelectDrawable);
            } else {
                mTopImageIcon.setImageDrawable(mUnSelectDrawable);
            }
        }

    }

    void updateTextWidget() {
        updateTextColor();
        updateTopImage();
    }

    public HBadgeRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public HBadgeRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    OnCheckChangeListener mOnCheckChangeListener;
    private HImageLoader mImageLoader;

    public void setImageLoader(HImageLoader imageLoader) {
        mImageLoader = imageLoader;
        update();
    }

    public OnCheckChangeListener getOnCheckChangeListener() {
        return mOnCheckChangeListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        mOnCheckChangeListener = onCheckChangeListener;
    }

    public interface OnCheckChangeListener {
        void onCheckChange(HBadgeRadioButton hb, boolean isCheck);
    }

    /**
     * 设置文字展示还是隐藏
     *
     * @param visible One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
     * @return 当前实例
     */
    public HBadgeRadioButton setTextVisibility(@Visibility int visible) {
        if (textView != null) {
            textView.setVisibility(visible);
        }
        return this;
    }

    /**
     * 文字是否显示
     *
     * @return Visibility
     */
    public int getTextVisibility() {
        return textView.getVisibility();
    }

}